/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OverviewProduct.java
 *
 * Created on Mar 3, 2010, 1:11:15 PM
 */

package dat215projekt.gui.components;

import dat215projekt.data.IMatDataHandlerExtended;
import java.awt.CardLayout;
import java.awt.Color;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import se.chalmers.cse.dat215.project.ShoppingItem;

/**
 *
 * @author ludwigk
 */
public class OverviewProduct extends javax.swing.JPanel {

    private ShoppingItem item;
    private static final Color rowStripeColor_1 = new Color(240,245,250);
    private static final Color rowStripeColor_2 = new Color(255,255,255);

    private static final IMatDataHandlerExtended handler = IMatDataHandlerExtended.getInstance();
    
    private static final NumberFormat moneyFormat;
    static {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        symbols.setGroupingSeparator(' ');

        moneyFormat = new DecimalFormat("###,##0.00 kr",symbols);
    }

    /** Creates new form OverviewProduct */
    public OverviewProduct(ShoppingItem p, int num) {
        initComponents();

        this.item = p;


        jLabel5.setText("Antal ("+p.getProduct().getUnitSuffix()+"):");
        jLabel6.setText("Antal ("+p.getProduct().getUnitSuffix()+"):");
        nameLabel.setText(p.getProduct().getName());
        priceLabel.setText(moneyFormat.format(p.getTotal()));
        imageLabel.setIcon(handler.getImageIcon(p.getProduct(), 120, 90));
        amountTextField.setText((int)p.getAmount()+"");
        amountLabel.setText((int)p.getAmount()+"");

        // Set background color for row-striping
        if(num % 2 == 1){
            setBackground(rowStripeColor_1);
        }
        else{
            setBackground(rowStripeColor_2);
        }

        // Set favorite button state
        favoriteButton.setSelected(handler.isFavorite(item.getProduct()));

        // Set favorite help text
        if(!favoriteButton.isSelected())
            favoriteHelpText.setText("<html><center>Lägg till favorit</center></html>");
        else
            favoriteHelpText.setText("<html><center>Ta bort favorit</center></html>");
        
        // Set card
        ((CardLayout)cardPanel.getLayout()).show(cardPanel,"card2");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        imageLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();
        cardPanel = new javax.swing.JPanel();
        editProductPanel = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        amountTextField = new javax.swing.JTextField();
        setAmountButton = new javax.swing.JButton();
        noEditProductPanel = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        editAmountButton = new javax.swing.JButton();
        amountLabel = new javax.swing.JLabel();
        deleteButton = new javax.swing.JToggleButton();
        jLabel7 = new javax.swing.JLabel();
        favoriteButton = new javax.swing.JToggleButton();
        favoriteHelpText = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(32000, 114));
        setName("Form"); // NOI18N

        imageLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        imageLabel.setMaximumSize(new java.awt.Dimension(120, 90));
        imageLabel.setMinimumSize(new java.awt.Dimension(120, 90));
        imageLabel.setName("imageLabel"); // NOI18N
        imageLabel.setPreferredSize(new java.awt.Dimension(120, 90));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setMaximumSize(new java.awt.Dimension(400, 135));
        jPanel1.setMinimumSize(new java.awt.Dimension(135, 135));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getResourceMap(OverviewProduct.class);
        nameLabel.setFont(resourceMap.getFont("nameLabel.font")); // NOI18N
        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        priceLabel.setFont(resourceMap.getFont("priceLabel.font")); // NOI18N
        priceLabel.setText(resourceMap.getString("priceLabel.text")); // NOI18N
        priceLabel.setName("priceLabel"); // NOI18N

        cardPanel.setName("cardPanel"); // NOI18N
        cardPanel.setOpaque(false);
        cardPanel.setLayout(new java.awt.CardLayout());

        editProductPanel.setName("editProductPanel"); // NOI18N
        editProductPanel.setOpaque(false);

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        amountTextField.setFont(resourceMap.getFont("amountTextField.font")); // NOI18N
        amountTextField.setText(resourceMap.getString("amountTextField.text")); // NOI18N
        amountTextField.setName("amountTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getActionMap(OverviewProduct.class, this);
        setAmountButton.setAction(actionMap.get("addSome")); // NOI18N
        setAmountButton.setFont(resourceMap.getFont("setAmountButton.font")); // NOI18N
        setAmountButton.setText(resourceMap.getString("setAmountButton.text")); // NOI18N
        setAmountButton.setToolTipText(resourceMap.getString("setAmountButton.toolTipText")); // NOI18N
        setAmountButton.setName("setAmountButton"); // NOI18N
        setAmountButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setAmountButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout editProductPanelLayout = new javax.swing.GroupLayout(editProductPanel);
        editProductPanel.setLayout(editProductPanelLayout);
        editProductPanelLayout.setHorizontalGroup(
            editProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(editProductPanelLayout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(amountTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(setAmountButton))
        );
        editProductPanelLayout.setVerticalGroup(
            editProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(editProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(amountTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(setAmountButton))
        );

        cardPanel.add(editProductPanel, "card1");

        noEditProductPanel.setToolTipText(resourceMap.getString("noEditProductPanel.toolTipText")); // NOI18N
        noEditProductPanel.setName("noEditProductPanel"); // NOI18N
        noEditProductPanel.setOpaque(false);

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        editAmountButton.setAction(actionMap.get("addSome")); // NOI18N
        editAmountButton.setFont(resourceMap.getFont("editAmountButton.font")); // NOI18N
        editAmountButton.setText(resourceMap.getString("editAmountButton.text")); // NOI18N
        editAmountButton.setName("editAmountButton"); // NOI18N
        editAmountButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editAmountButtonActionPerformed(evt);
            }
        });

        amountLabel.setText(resourceMap.getString("amountLabel.text")); // NOI18N
        amountLabel.setName("amountLabel"); // NOI18N

        javax.swing.GroupLayout noEditProductPanelLayout = new javax.swing.GroupLayout(noEditProductPanel);
        noEditProductPanel.setLayout(noEditProductPanelLayout);
        noEditProductPanelLayout.setHorizontalGroup(
            noEditProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(noEditProductPanelLayout.createSequentialGroup()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(amountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                .addComponent(editAmountButton))
        );
        noEditProductPanelLayout.setVerticalGroup(
            noEditProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(noEditProductPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addComponent(jLabel6)
                .addComponent(amountLabel)
                .addComponent(editAmountButton))
        );

        cardPanel.add(noEditProductPanel, "card2");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nameLabel)
                .addGap(57, 57, 57)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cardPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(75, 75, 75))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(jLabel3)
                    .addComponent(priceLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(cardPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        deleteButton.setIcon(resourceMap.getIcon("deleteButton.icon")); // NOI18N
        deleteButton.setToolTipText(resourceMap.getString("deleteButton.toolTipText")); // NOI18N
        deleteButton.setContentAreaFilled(false);
        deleteButton.setName("deleteButton"); // NOI18N
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N
        jLabel7.setPreferredSize(new java.awt.Dimension(92, 15));

        favoriteButton.setIcon(resourceMap.getIcon("favoriteButton.icon")); // NOI18N
        favoriteButton.setContentAreaFilled(false);
        favoriteButton.setName("favoriteButton"); // NOI18N
        favoriteButton.setSelectedIcon(resourceMap.getIcon("favoriteButton.selectedIcon")); // NOI18N
        favoriteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                favoriteButtonActionPerformed(evt);
            }
        });

        favoriteHelpText.setFont(resourceMap.getFont("favoriteHelpText.font")); // NOI18N
        favoriteHelpText.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        favoriteHelpText.setText(resourceMap.getString("favoriteHelpText.text")); // NOI18N
        favoriteHelpText.setName("favoriteHelpText"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(deleteButton)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(favoriteButton)
                    .addComponent(favoriteHelpText))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 106, Short.MAX_VALUE)
                    .addComponent(imageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(favoriteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(favoriteHelpText))))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void setAmountButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setAmountButtonActionPerformed
        ((CardLayout)cardPanel.getLayout()).next(cardPanel);

        try{
            int newAmount = Integer.parseInt(amountTextField.getText());
            handler.changeShoppingItemAmount(item, newAmount - (int)item.getAmount());
        }
        catch(Exception e){
            // ....
        }

        amountLabel.setText((int)item.getAmount()+"");
        priceLabel.setText(moneyFormat.format(item.getTotal()));
        
    }//GEN-LAST:event_setAmountButtonActionPerformed

    private void editAmountButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editAmountButtonActionPerformed
        ((CardLayout)cardPanel.getLayout()).next(cardPanel);
    }//GEN-LAST:event_editAmountButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        handler.removeFromShoppingCart(item);
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void favoriteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_favoriteButtonActionPerformed
       if(favoriteButton.isSelected()) // Add as favorite
           handler.addFavorite(item.getProduct());
       else // Remove from favorites
           handler.removeFavorite(item.getProduct());

       if(!favoriteButton.isSelected())
            favoriteHelpText.setText("<html><center>Lägg till favorit</center></html>");
        else
            favoriteHelpText.setText("<html><center>Ta bort favorit</center></html>");
    }//GEN-LAST:event_favoriteButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel amountLabel;
    private javax.swing.JTextField amountTextField;
    private javax.swing.JPanel cardPanel;
    private javax.swing.JToggleButton deleteButton;
    private javax.swing.JButton editAmountButton;
    private javax.swing.JPanel editProductPanel;
    private javax.swing.JToggleButton favoriteButton;
    private javax.swing.JLabel favoriteHelpText;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JPanel noEditProductPanel;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JButton setAmountButton;
    // End of variables declaration//GEN-END:variables

}
